package com.daisuxia.payRoute;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.daisuxia.risk.service.IOutOrdersService;
import com.daisuxia.risk.service.OutOrdersService;
import com.daisuxia.rongbao.RongBaoAllUtil;
import com.daisuxia.web.pojo.OutOrders;
import com.daisuxia.web.pojo.Repayment;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.pojo.UserCardInfo;
import com.daisuxia.web.util.DateUtil;
import com.daisuxia.web.util.GenerateNo;

/**
 * Created by dhl on 2017/5/18.
 */

public class RongBaoPay implements IPayCommons {
	private IOutOrdersService outOrdersService;

	private IPayCommons nextIPayCommons;

	public RongBaoPay(IOutOrdersService outOrdersService) {
		this.outOrdersService = outOrdersService;
	}

	@Override
	public void setNextSuccessor(IPayCommons iPayCommons) {
		this.nextIPayCommons = iPayCommons;
	}

	@Override
	public Map handleOrder(Repayment re, User user, UserCardInfo userCardInfo, String transId) throws IOException, URISyntaxException {
		try {
			String orderNo = GenerateNo.payRecordNo(user.getId());
			 transId = GenerateNo.payRecordNo();
			
			 //用户协议号 单笔白名单提交、单笔代扣时需要同一个
		    String agreement_no = GenerateNo.nextOrdId();
		    
			Map result = RongBaoAllUtil.dkWhite(re, user, userCardInfo, orderNo,agreement_no);
			result.put("resp_code", result.get("result_code").toString());
			
			String signParam = result.get("qbm_signParam").toString();//请求参数
			String ourOrders = result.get("payCutType").toString();//支付机构
			String resultParam = result.get("qbm_resultParam").toString();//支付机构
			//保存请求记录
			OutOrders outOrders = new OutOrders();
			String tablelastName = DateUtil.getDateFormat(new Date(), "yyyyMMdd");
			outOrders.setUserId(user.getId());
			outOrders.setAssetOrderId(re.getId());
			outOrders.setOrderType(OutOrders.TYPE_RONGBAO);
			outOrders.setOrderNo(orderNo);
			outOrders.setAct(OutOrders.ACT_GRANT);
			outOrders.setReqParams(signParam);
			outOrders.setNotifyParams("");
			outOrders.setReturnParams(resultParam);
			outOrders.setStatus(OutOrders.STATUS_SUC);
			outOrders.setTablelastName(tablelastName);
			outOrdersService.insertByTablelastName(outOrders);
			//融宝技术反馈：同步本应该返回0000
			if ("0000".equals(result.get("result_code").toString())) {
				Map<String, Object> dkCollResult = RongBaoAllUtil.dkColl(re, user, userCardInfo, transId, agreement_no);
				dkCollResult.put("resp_code", dkCollResult.get("result_code").toString());
				signParam = dkCollResult.get("qbm_signParam").toString();//请求参数
				ourOrders = dkCollResult.get("payCutType").toString();//
				resultParam = dkCollResult.get("qbm_resultParam").toString();//返回参数
				
				outOrders = new OutOrders();
				outOrders.setUserId(user.getId());
				outOrders.setAssetOrderId(re.getId());
				outOrders.setOrderType(OutOrders.TYPE_RONGBAO);
				outOrders.setOrderNo(transId);	                            //商户订单号也和批次号一致 便于查账
				outOrders.setAct(OutOrders.ACT_DEBIT);
				outOrders.setReqParams(signParam);
				outOrders.setReturnParams(resultParam);
				outOrders.setNotifyParams("");
				outOrdersService.insert(outOrders);
				//融宝技术反馈：同步本应该返回0000，也有可能返回3083的,3083就在处理中来处理
				if ("0000".equals(dkCollResult.get("result_code").toString())||nextIPayCommons==null) {
					dkCollResult.put("payCutType", OutOrders.TYPE_RONGBAO);//代扣类型
					return dkCollResult;
					//如果是 处理中的状态 3081 1101 处理中 3083 提交成功
				}else if("3081".equals(dkCollResult.get("result_code").toString())||
						"3083".equals(dkCollResult.get("result_code").toString())||
						"1101".equals(dkCollResult.get("result_code").toString())
						){
					return dkCollResult;
				}else if(nextIPayCommons == null){
					return dkCollResult;
				}else{
					outOrders.setStatus(OutOrders.STATUS_OTHER);
					outOrdersService.updateByOrderNo(outOrders);
				}
				
			}
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		return nextIPayCommons.handleOrder(re, user, userCardInfo, transId);
	}
}
